#include "wtlpch.h"

#include "resource.h"

#include "TARDownView.h"
#include "MainFrm.h"

CAppModule _Module;
CTARConfigs _ConfigApp;

int Run(LPTSTR lpstrCmdLine = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
	CMessageLoop theLoop;
	_Module.AddMessageLoop(&theLoop);

	CMainFrame wndMain;

	if(wndMain.CreateEx() == NULL)
	{
		ATLTRACE(_T("Main window creation failed!\n"));
		return 0;
	}

	wndMain.ShowWindow(nCmdShow);

	int nRet = theLoop.Run();

	_Module.RemoveMessageLoop();
	return nRet;
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
	//We Use STA, so the default init is enough
	HRESULT hRes = ::CoInitialize(NULL);
	ATLASSERT(SUCCEEDED(hRes));

	// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
	::DefWindowProc(NULL, 0, 0, 0L);

	AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES);	// add flags to support other controls

	hRes = _Module.Init(NULL, hInstance);
	ATLASSERT(SUCCEEDED(hRes));

	hRes = _ConfigApp.LoadConfigFromXML();

	CCURLHelper::InitCURLWin32();	//Init curl, only once	

	int nRet = Run(lpstrCmdLine, nCmdShow);

	CCURLHelper::CleanCURLWin32();	//Clean curl, only once

	_ConfigApp.AppSettings.SaveSettings();

	_Module.Term();
	::CoUninitialize();

	return nRet;
}
